import os
import json

def process_jsonl_files():
    # 获取父文件夹路径
    parent_dir = os.path.dirname(os.path.abspath('.'))
    
    # 获取父文件夹下所有.jsonl文件
    jsonl_files = [f for f in os.listdir(parent_dir) if f.endswith('.jsonl')]
    
    print(f"在父文件夹 {parent_dir} 中找到 {len(jsonl_files)} 个jsonl文件")
    
    for file_name in jsonl_files:
        file_path = os.path.join(parent_dir, file_name)
        output_lines = []
        
        print(f"正在处理文件: {file_path}")
        
        with open(file_path, 'r', encoding='utf-8') as f:
            for line_num, line in enumerate(f, 1):
                line = line.strip()
                if not line:  # 跳过空行
                    continue
                    
                try:
                    data = json.loads(line)
                except json.JSONDecodeError:
                    print(f"警告: {file_name} 第{line_num}行不是有效的JSON格式，已跳过")
                    continue
                
                # 删除id键（如果存在）
                if 'id' in data:
                    del data['id']
                
                # 确保只保留messages键
                if 'messages' in data:
                    # 如果已经有messages键，保留它
                    processed_data = {'messages': data['messages']}
                else:
                    # 如果没有messages键，将整个内容作为messages的值
                    processed_data = {'messages': data}
                
                # 转换为JSON字符串
                new_line = json.dumps(processed_data, ensure_ascii=False)
                output_lines.append(new_line)
        
        # 写回原文件
        with open(file_path, 'w', encoding='utf-8') as f:
            for line in output_lines:
                f.write(line + '\n')
        
        print(f"已处理文件: {file_name}，共处理{len(output_lines)}行")

if __name__ == "__main__":
    process_jsonl_files()